package com.cucn.ingmall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.cucn.ingmall.product.entity.AttrEntity;
import com.cucn.ingmall.product.service.AttrAttrgroupRelationService;
import com.cucn.ingmall.product.service.AttrService;
import com.cucn.ingmall.product.service.CategoryService;
import com.cucn.ingmall.product.vo.AttrGroupRelationVO;
import com.cucn.ingmall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cucn.ingmall.product.entity.AttrGroupEntity;
import com.cucn.ingmall.product.service.AttrGroupService;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.R;


/**
 * 属性分组
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 17:07:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * /product/attrgroup/{attrgroupId}/noattr/relation
     *
     * @param params      请求参数
     * @param attrgroupId 属性分组id
     * @return 统一结果返回
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.getNoRelationAttr(params, attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组的关联的所有属性
     * //product/attrgroup/{attrgroupId}/attr/relation
     *
     * @param attrgroupId 属性分组id
     * @return 统一结果返回-attrgroupId对应的所有关联的属性的信息
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", entities);
    }

    /**
     * 通过分类id获取属性分组
     *
     * @param params    请求参数
     * @param catelogId 三级分类id
     * @return 统一结果返回
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 根据属性分组id查询属性分组的详情
     *
     * @param attrGroupId 属性分组id
     * @return 统一结果返回-属性分组详情
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //得到属性分组的第三级分类id
        Long catelogId = attrGroup.getCatelogId();
        //根据第三级分类id查询出完整分类路径
        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 分类id
     * @return 统一结果返回-分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组下的所有属性
        List<AttrGroupWithAttrsVO> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data", vos);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 添加属性与属性分组关联关系(批量添加)
     *
     * @param vos 属性与属性分组的关联VO对象集合
     * @return 统一结果返回
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     * /product/attrgroup/attr/relation/delete
     * 请求参数:[{"attrId":1,"attrGroupId":2}]
     *
     * @param vos 需删除的关联关系
     * @return 统一结果返回
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrGroupRelationVO> vos) {
        attrService.deleteRelation(vos);

        return R.ok();
    }

}
